package com.li.service;

import com.li.dao.MemberMapper;
import com.li.pojo.Member;
import com.li.pojo.User;
import com.li.util.MyStringUtil;
import com.li.util.MyTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MemberServiceImpl implements MemberService{

    @Autowired
    private MemberMapper memberMapper;

    public Member checkAddMember(String name){
       return memberMapper.checkAddMember(name);
    }

    @Override
    public Member selectOne(int id) {
        return memberMapper.selectOne(id);
    }

    @Override
    public List<Member> selectMember(Map<String, String> map) {
        //数据筛选 清理
        Map<String,Object> map1 = new HashMap<>();
        String id=map.get("id");
        String page=map.get("page");
        String pageSize=map.get("pageSize");
        String name=map.get("name");
        String work=map.get("work");
        if (MyStringUtil.isNullOrEmpty(id)){
            map1.put("id",0);
        }else {
            int newId=Integer.parseInt(id);
            map1.put("id",newId);
        }

        if (!MyStringUtil.isNullOrEmpty(name)){
            map1.put("name","%"+name+"%");
        }
        if (!MyStringUtil.isNullOrEmpty(work)){
            map1.put("work","%"+work+"%");
        }
        int newPage = Integer.parseInt(page);
        int newPageSize = Integer.parseInt(pageSize);
        map1.put("page",(newPage-1)*newPageSize);
        map1.put("pageSize",newPageSize);
        return memberMapper.selectMembers(map1);
    }

    @Override
    public long count(Map<String, String> map) {
        //数据筛选 清理
        Map<String,Object> map1 = new HashMap<>();
        String id=map.get("id");
        String page=map.get("page");
        String pageSize=map.get("pageSize");
        String name=map.get("name");
        String work=map.get("work");
        if (MyStringUtil.isNullOrEmpty(id)){
            map1.put("id",0);
        }else {
            int newId=Integer.parseInt(id);
            map1.put("id",newId);
        }
//        动态sql 模糊查询
        if (!MyStringUtil.isNullOrEmpty(name)){
            map1.put("name","%"+name+"%");
        }
        if (!MyStringUtil.isNullOrEmpty(work)){
            map1.put("work","%"+work+"%");
        }
        return memberMapper.count(map1);
    }

    @Override
    public int updateMember(Member member) {
        return memberMapper.updateMember(member);
    }

    @Override
    public int addMember(Member member) {
        Member ckm = memberMapper.checkAddMember(member.getName());

        if (ckm!=null){
            return 0;
        }
        member.setId(0);
        if (MyStringUtil.isNullOrEmpty(member.getDate())){
            member.setDate(MyTimeUtil.getCurrentTime());
        }

        member.setIsDelete("0");
        return memberMapper.addMember(member);
    }

    @Override
    public int deleteMember(List<Integer> ids) {
        return memberMapper.deleteMember(ids);
    }
}
